package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.OrderItem;
import ex.com.jm.food.service.CompanyOrderItemService;
import ex.com.jm.food.utils.JsonUtils;

/**
 * 订单项管理action
 * @author wangshenfeng
 *
 */
public class CompanyOrderItemAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private CompanyOrderItemService companyOrderItemService;
	private OrderItem orderItem;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String orderItemId;
	private String orderId;
	private String menuName;
	private String order;
	private String sort;
	public CompanyOrderItemService getCompanyOrderItemService() {
		return companyOrderItemService;
	}
	public void setCompanyOrderItemService(
			CompanyOrderItemService companyOrderItemService) {
		this.companyOrderItemService = companyOrderItemService;
	}
	public OrderItem getOrderItem() {
		return orderItem;
	}
	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getOrderItemId() {
		return orderItemId;
	}
	public void setOrderItemId(String orderItemId) {
		this.orderItemId = orderItemId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	
	/**
	 * 到达列表页面
	 * @return
	 */
	public String orderItemList(){
		return "orderItemList";
		
	}
	/**
	 * 得到数据
	 */
	public void getOrderItems(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<OrderItem> orderItems=this.getCompanyOrderItemService().getOrderItems(offset, pageRow, orderId, menuName, order, sort);
		System.err.println(orderItems.size());
		JSONArray jootem=JSONArray.fromObject(orderItems);
		totalCount=this.getCompanyOrderItemService().orderItemCount(orderId, menuName);
		JSONObject jo=JsonUtils.buildJson(jootem, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close(); 
	}
	
	/**
	 * 编辑
	 * @return
	 */
	public String editOrderItem(){
		if(orderItemId!=null){
			orderItem=this.getCompanyOrderItemService().editOrderItem(orderItemId);
		}
		return "edit";
	}
	/**
	 * 跟新
	 * @return
	 */
	public String updateOrderItem(){
		orderItem.setProfit(orderItem.getMenuNowPrice()*orderItem.getMenuNum());
		orderItem.setOrderId(this.getCompanyOrderItemService().getOrder(orderId));
		this.getCompanyOrderItemService().updateOrderItem(orderItem);
		return "list";
		
	}
	/**
	 * 删除
	 * @return
	 */
	public String delOrderItem(){
		this.getCompanyOrderItemService().delOrderItem(orderItemId);
		return "list";
	}

}
